#coding=utf-8

from common.import_common import *

from relation.models import Relation

def friends(request, template_name="relation/friends.html"):
    user = request.user
    relations = Relation.objects.filter(from_user=user, type=Relation.FRIEND)
    return render_to_response(template_name, 
                          {
                            "relations":relations,
                          },
                            context_instance=RequestContext(request))
    
def confirm_list(request, template_name="relation/confirm_list.html"):
    user = request.user
    relations = Relation.objects.filter(to_user=user, type=Relation.REQ_FRIEND)
    return render_to_response(template_name, 
                          {
                            "relations":relations,
                          },
                            context_instance=RequestContext(request))


def add(request, uid):
    people = get_object_or_404(User, pk=uid)
    if not Relation.objects.isblock(from_user=people, to_user=request.user):
        try:
            relation = Relation.objects.get(from_user=request.user, to_user=people)
            if relation != Relation.REQ_FRIEND:
                relation.type = Relation.REQ_FRIEND
                relation.save()
        except Relation.DoesNotExist:
            relation = Relation(from_user=request.user, to_user=people, type=Relation.REQ_FRIEND)
            relation.save()
    return HttpResponseRedirect("/people/%d"%people.pk)
    
    
    
def remove(request, uid):
    import pdb;pdb.set_trace()
    people = get_object_or_404(User, pk=uid)
    try:
        relation1 = Relation.objects.get(from_user=request.user, to_user=people)
        relation2 = Relation.objects.get(from_user=people, to_user=request.user)
        if relation1 != Relation.BLOCK:
            relation1.delete()
        if relation2 != Relation.BLOCK:
            relation2.delete()            
    except Relation.DoesNotExist:
        pass    

    return HttpResponseRedirect("/people/%d"%people.pk)    


def confirm(request, uid):
    people = get_object_or_404(User, pk=uid)
    try:
        relation = Relation.objects.get(from_user=people, to_user=request.user)
        if relation.type == Relation.REQ_FRIEND:
            relation.type = Relation.FRIEND
            relation.save()
            relation = Relation(from_user=request.user, to_user=people, type=Relation.FRIEND)
            relation.save()
    except Relation.DoesNotExist:
        pass
    return HttpResponseRedirect("/relation/confirm_list")
    
def cancel(request, uid):
    people = get_object_or_404(User, pk=uid)
    try:
        relation = Relation.objects.get(from_user=people, to_user=request.user)
        if relation.type == Relation.REQ_FRIEND:
            relation.delete()
    except Relation.DoesNotExist:
        pass
    return HttpResponseRedirect("/relation/confirm_list")
    
    
def block(request, uid):
    people = get_object_or_404(User, pk=uid)
    try:
        relation = Relation.objects.get(from_user=request.user, to_user=people)
        if relation != Relation.BLOCK:
            relation.type = Relation.BLOCK
            relation.save()
    except Relation.DoesNotExist:
        relation = Relation(from_user=request.user, to_user=people, type=Relation.BLOCK)
        relation.save()
    return HttpResponseRedirect("/people/%d"%people.pk)
    
